﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Data;
using System.Data.SqlClient;

public partial class Service_Subject : System.Web.UI.Page
{
    public double pi = 0.0174532925199433;
    public double gps_distince(double lat1, double long1, double lat2, double long2)
    {

        double dlong = (long2 - long1) * pi;

        double dlat = (lat2 - lat1) * pi;

        double plong = Math.Sin(dlong / 2.0);

        double plat = Math.Sin(dlat / 2.0);

        double a = Math.Pow(plong, 2) + Math.Cos(lat1 * pi) * Math.Cos(lat2 * pi) * Math.Pow(plat, 2);

        double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));

        double distance = 6367 * c;

        return distance*10000;

    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request.QueryString["subid"]) && !string.IsNullOrEmpty(Request.QueryString["lat"]) && !string.IsNullOrEmpty(Request.QueryString["lng"]))
        {
            double gps = 0;
            using (MyDb.Db db = new MyDb.Db())
            {
                using (SqlDataReader rs = db.getList(string.Format("select SubjectID,Source from vw_Subject where SubjectID='{0}' order by SubjectID", Request.QueryString["subid"])))
                {
                    if(rs.Read())
                    {
                        if (rs["Source"].ToString() == "Bus")
                        {
                            using (SqlDataReader rs2 = db.getList(string.Format("select b.Latitude,b.Longitude from bus a left join Busevent b on b.BusID=a.BusID where SubjectID='{0}'",
                                rs["SubjectID"])))
                            {
                                if (rs2.Read())
                                {
                                    if (!string.IsNullOrEmpty(rs2["Latitude"].ToString()))
                                    {
                                        gps = gps_distince(Convert.ToDouble(Request.QueryString["lat"]), Convert.ToDouble(Request.QueryString["lng"]), Convert.ToDouble(rs2["Latitude"]), Convert.ToDouble(rs2["Longitude"]));
                                    }
                                    Response.Write(string.Format("{{\"subject\":{{\"distance\":\"{2}\",\"unit\":\"公尺\",\"source\":\"{0}\",\"same_loc\":\"{1}\"}}}}", rs["Source"], gps<=300?1:0,gps)); 
                                }
                                rs2.Close();
                            }
                        }
                        else
                        {
                            using (SqlDataReader rs2 = db.getList(string.Format("select Lat,Lng from Shop where SubjectID='{0}'",
                                rs["SubjectID"])))
                            {
                                if (rs2.Read())
                                {
                                    gps = gps_distince(Convert.ToDouble(Request.QueryString["lat"]), Convert.ToDouble(Request.QueryString["lng"]), Convert.ToDouble(rs2["Lat"]), Convert.ToDouble(rs2["Lng"]));

                                    Response.Write(string.Format("{{\"subject\":{{\"distance\":\"{2}\",\"unit\":\"公尺\",\"source\":\"{0}\",\"same_loc\":\"{1}\"}}}}", rs["Source"], gps <= 300 ? 1 : 0,gps));
                                }
                                rs2.Close();
                            }
                        }
                        
                    }
                    rs.Close();
                }
                
            }
        }
        Response.End();
    }
}
